package com.open.capacity.user.service.impl;

import com.open.capacity.common.model.Group;
import com.open.capacity.common.model.SysUser;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.user.dao.GroupDao;
import com.open.capacity.user.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    private GroupDao groupDao;

    @Override
    public PageResult<Group> selectGroupByTeacher(Map<String, Object> params) {
        int total = groupDao.selectGroupCountByTeacher(params);
        if(params.containsKey("page")){
            PageUtil.pageParamConver(params, true);
        }else {
            params.put("page",null);
            params.put("limit",null);
        }
        List<Group> list = groupDao.selectGroupByTeacher(params);
        return PageResult.<Group>builder().data(list).code(0).count((long) total).build();//收集所有数据
    }

    @Override
    public Result addGroup(Group group) {
        if (groupDao.selectGroupByName(group.getGpname()).size()>0){
            Result.failed("小组名称重复，请重新输入");
        }
        return Result.succeed(groupDao.addGroup(group),"添加成功");
    }

    @Override
    public Result deleteGroupById(int id) {
        return Result.succeed(groupDao.deleteGroupById(id),"删除成功");
    }

    @Override
    public Result updateGroupType(Map<String, Object> params) {
        return Result.succeed(groupDao.updateGroupType(params),"修改状态成功");
    }

    @Override
    public Result getCheckGroupMember(int id) {
        Map<String,Object> map = new HashMap<>();
        map.put("noGroupMember",groupDao.getNoGroupUser());
        map.put("groupMember",groupDao.getGroupUser(id));
        return Result.succeed(map,"查询成功");
    }

    @Override
    public Result addGroupMember(Map param) {
        groupDao.addGroupMember(param);
        int number = groupDao.selectUserByGroup((Integer) param.get("grouid")).size();
        return Result.succeed(groupDao.updateGroupNumber((Integer) param.get("grouid"),number),"查询成功");
    }

    @Override
    public Result removeGroupMember(Map param) {
        groupDao.removeGroupMember(param);
        int number = groupDao.selectUserByGroup((Integer) param.get("grouid")).size();
        return Result.succeed(groupDao.updateGroupNumber((Integer) param.get("grouid"),number),"查询成功");
    }

    @Override
    public Result updateGroup(Group group) {
        return Result.succeed(groupDao.updateGroup(group),"修改成功");
    }

    @Override
    public Result selectGroupByMember(Long id) {
        return Result.succeed(groupDao.selectGroupByMember(id),"查询成功");
    }

    @Override
    public PageResult selectUserByGroup(Map<String,Object> params) {
        int total = groupDao.selectUserCountByGroup(params);
        if(params.containsKey("page")){
            PageUtil.pageParamConver(params, true);
        }else {
            params.put("page",null);
            params.put("limit",null);
        }
        List<SysUser> list = groupDao.selectUserByGroupId(params);
        return PageResult.<SysUser>builder().data(list).code(0).count((long) total).build();//收集所有数据
    }

    @Override
    public Result getGroup(Map<String, Object> params) {
        List<Group> list = groupDao.getGroup(params);
        return Result.succeed(list,"查询成功");
    }

    /**
     * 根据班主任id获取所有成员
     * @param teachid
     * @return
     */
    @Override
    public List<SysUser> getGroupUserByTeacher(Long teachid) {
        return groupDao.getGroupUserByTeacher(teachid);
    }
}
